package com.source.module_ipc_server.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.source.module_ipc_server.base.Constant;


/**
 * @author Created by wsl
 * @des 自身App内使用的服务
 * @date 2022/6/8
 */
public class LocalService extends Service {
    private static final String Tag = "LocalService_";
    private MyBinder myBinder;
    private int count;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        new Thread() {
            @Override
            public void run() {
                while (count < 10) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                    if (myBinder != null && myBinder.mCallback != null) {
                        myBinder.mCallback.call(count);
                    }
                }
            }
        }.start();
        Log.e(Constant.TAG, Tag + "onBind");
        return myBinder;
    }

    @Override
    public void onCreate() {
        myBinder = new MyBinder();
        super.onCreate();
        Log.e(Constant.TAG, Tag + "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(Constant.TAG, Tag + "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    public static class MyBinder extends Binder {
        private MyCallback mCallback;

        public interface MyCallback {
            void call(int num);
        }

        public void addCallBack(MyCallback callback) {
            this.mCallback = callback;
        }

        public void startDownload() {
            Log.e(Constant.TAG, Tag + "startDownload");
        }

        public void downloading() {
            Log.e(Constant.TAG, Tag + "downloading");
        }

        public void stopDownload() {
            Log.e(Constant.TAG, Tag + "stopDownload");
        }

        public void completeDownload() {
            Log.e(Constant.TAG, Tag + "completeDownload");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(Constant.TAG, Tag + "onDestroy");
    }
}
